package dianmobile.eistarandroid.ui.activities;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.ImageButton;
import android.widget.TextView;

import java.util.HashMap;
import java.util.Map;

import de.hdodenhof.circleimageview.CircleImageView;
import dianmobile.eistarandroid.ByhhAndroidApplication;
import dianmobile.eistarandroid.R;
import dianmobile.eistarandroid.network.request.MailContentRequest;
import dianmobile.eistarandroid.network.request.RequestCallback;
import dianmobile.eistarandroid.network.request.RequestFactory;
import dianmobile.eistarandroid.ui.views.SwipeActivity;
import dianmobile.eistarandroid.utils.ConstantsData;
import dianmobile.eistarandroid.utils.DataUtils;

public class MailContentActivity extends SwipeActivity {

    private Map<String, String> params = new HashMap<>();
    private String userID;

    private TextView contentView;
    private TextView userIDView;
    private TextView mailTitleView;
    private TextView sendTimeView;
    private CircleImageView userHeader;
    private ImageButton backBtn;
    private ImageButton sendBtn;
    private TextView activityTitle;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        ByhhAndroidApplication application = (ByhhAndroidApplication)getApplication();
        application.getActivityList().add(this);

        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_mail_content);

        Intent intent = getIntent();
        params.put("file", intent.getStringExtra("file"));
        params.put("num", intent.getStringExtra("num"));
//        params.put("backup", intent.getStringExtra("backup"));
        params.put("xml", "1");
        userID = intent.getStringExtra("userID");

        initView(intent);

        String url = DataUtils.getUserHeadUrlFromName(userID);
        RequestFactory.makeNetworkImage(userHeader, url, R.drawable.default_face, R.drawable.default_face);

        getContent();
        backBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                MailContentActivity.this.finish();
            }
        });
        sendBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MailContentActivity.this, PostArticleActivity.class);
                intent.putExtra(ConstantsData.EXTRA_POST_TYPE, ConstantsData.POST_TYPE_MAIL);
                intent.putExtra(ConstantsData.EXTRA_USER_NAME, userID);
                MailContentActivity.this.startActivity(intent);
            }
        });
    }

    @Override
    public void onDestroy() {
        ByhhAndroidApplication application = (ByhhAndroidApplication)getApplication();
        application.getActivityList().remove(this);
        super.onDestroy();
    }

    private void getContent() {
        final ProgressDialog progressDialog= new ProgressDialog(this);
        progressDialog.setMessage("加载中");
        progressDialog.show();

        MailContentRequest.execute(this, params, new RequestCallback() {
            @Override
            public void onResult(Map<String, Object> result, String error) {
                progressDialog.dismiss();
                if(error == null) {
                    contentView.setText((String) result.get(ConstantsData.RESULT_DATA));
                } else {
                    contentView.setText("网络错误");
                }
            }
        });
    }

    private void initView(Intent intent) {
        backBtn = (ImageButton)findViewById(R.id.btn_back);
        sendBtn = (ImageButton)findViewById(R.id.btn_send);
        activityTitle = (TextView)findViewById(R.id.tv_activity_title);

        userIDView = (TextView)findViewById(R.id.tv_sender_name);
        mailTitleView = (TextView)findViewById(R.id.tv_mail_title);
        sendTimeView = (TextView)findViewById(R.id.tv_send_time);
        contentView = (TextView)findViewById(R.id.tv_mail_content);
        userHeader = (CircleImageView)findViewById(R.id.iv_author_head);

        activityTitle.setText("信件内容");
        userIDView.setText(userID);
        mailTitleView.setText(intent.getStringExtra("mailTitle"));
        sendTimeView.setText(intent.getStringExtra("time"));
    }

}
